//
//  AppDelegate.swift
//  HJWeiBo
//
//  Created by 黄珏 on 15/10/26.
//  Copyright © 2015年 黄珏. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultController()
        window?.makeKeyAndVisible()
        
        return true
    }
    
    func defaultController() -> UIViewController {
        if !HJUserAccount.isUerLogin {
            return HJmainViewController()
        }
        return isNewFeature() ? HJNewFeatureViewController() : HJwelcomeViewController()
    }
    
    func isNewFeature() -> Bool {
//        取到当前版本
        let currentVersionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = Double(currentVersionStr)
//        获取以前版本
        let sanBoxVersionKey = "sanBoxVersionKey"
        let sanBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sanBoxVersionKey)
        
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion!, forKey: sanBoxVersionKey)
//        NSUserDefaults.standardUserDefaults().synchronize()
        
        return currentVersion > sanBoxVersion
    }
    /**
    切换根控制器
    - parameter isMain: true: 表示切换到MainViewController, false: welcome
    */
    func switchRootController(isMain: Bool) {
        window?.rootViewController = isMain ? HJmainViewController() : HJwelcomeViewController()
    }
}

